<?php
/**
 * 《我的采购》实现类
 *
 * @author 孙晓晔
 * @version $Id: Purchase.class.php 804 2009-12-30 10:05:41Z sunxy $
 */
require_once(dirname(__FILE__) . '/Material.class.php');

class My extends Material {
	
	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'material/purchase/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['material'];
		if (isset($_SESSION['cart'])) {
			$cart = $_SESSION['cart'];
		}
		
		$cart[$id] = $id;
		
		$_SESSION['cart'] = $cart;
		
		$controller->assign('msg', "成功添加至采购单！");
		
		$this->findPost($controller, $request);		
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		
		$this->add($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);

		$controller->assign_by_ref('material', $this->id(Constant::tbl_material, $row['material']));

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'material/purchase/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$material = $request['material'];
		$amount = $request['amount'];
		$date = $request['date'];

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET amount = '%s', date = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $amount, $date, $_SESSION['id'], $status, $id);
		// debug($sql);
		$db->query($sql);

		if ($status == '2') {
			$fmt = "INSERT INTO %s(purchase, material, amount, createtime, modifytime) VALUES('%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, Constant::tbl_material_in, $id, $material, $amount);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign("msg", "修改成功");
		$controller->display($request, 'info');

		return true;
	}
}
?>
